package com.hig.onlineexam.service.impl;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.hig.exam.domain.ExamTaskManager;
import com.hig.exam.service.IExamTaskManagerService;
import com.hig.onlineexam.domain.ExamTaskAnswer;
import com.hig.onlineexam.mapper.ExamUserScoreMapper;
import com.hig.onlineexam.service.IExamTaskAnswerService;
import com.hig.utils.UUIDGenerator;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hig.onlineexam.mapper.ExamFinishAnswerMapper;
import com.hig.exam.domain.ExamTaskPerson;
import com.hig.exam.service.IExamTaskPersonService;
import com.hig.onlineexam.domain.ExamFinishAnswer;
import com.hig.onlineexam.domain.ExamUserScore;
import com.hig.onlineexam.service.IExamFinishAnswerService;
import com.hig.onlineexam.service.IExamUserScoreService;

/**
 * 考试成绩Service业务层处理
 * 
 * @author qnsdt
 * @date 2023-02-24
 */
@Service
public class ExamFinishAnswerServiceImpl implements IExamFinishAnswerService 
{
    @Autowired
    private ExamFinishAnswerMapper examFinishAnswerMapper;

    @Autowired
    private IExamUserScoreService examUserScoreService;
    
    @Autowired
    private IExamTaskPersonService examTaskPersonService;

@Autowired
private IExamTaskManagerService examTaskManagerService;
    @Autowired
    private IExamTaskAnswerService examTaskAnswerService;


    /**
     * 查询考试成绩
     * 
     * @param examCode 考试成绩ID
     * @return 考试成绩
     */
    @Override
    public ExamFinishAnswer selectExamFinishAnswerById(String examCode)
    {
        return examFinishAnswerMapper.selectExamFinishAnswerById(examCode);
    }

    /**
     * 查询考试成绩列表
     * 
     * @param examFinishAnswer 考试成绩
     * @return 考试成绩
     */
    @Override
    public List<ExamFinishAnswer> selectExamFinishAnswerList(ExamFinishAnswer examFinishAnswer)
    {
        return examFinishAnswerMapper.selectExamFinishAnswerList(examFinishAnswer);
    }

    /**
     * 新增考试成绩
     * 
     * @param examFinishAnswer 考试成绩
     * @return 结果
     */
    @Override
    public int insertExamFinishAnswer(ExamFinishAnswer examFinishAnswer)
    {
        return examFinishAnswerMapper.insertExamFinishAnswer(examFinishAnswer);
    }

    /**
     * 修改考试成绩
     * 
     * @param examFinishAnswer 考试成绩
     * @return 结果
     */
    @Override
    public int updateExamFinishAnswer(ExamFinishAnswer examFinishAnswer)
    {
        return examFinishAnswerMapper.updateExamFinishAnswer(examFinishAnswer);
    }

    /**
     * 批量删除考试成绩
     * 
     * @param examCodes 需要删除的考试成绩ID
     * @return 结果
     */
    @Override
    public int deleteExamFinishAnswerByIds(String[] examCodes)
    {
        return examFinishAnswerMapper.deleteExamFinishAnswerByIds(examCodes);
    }

    /**
     * 删除考试成绩信息
     * 
     * @param examCode 考试成绩ID
     * @return 结果
     */
    @Override
    public int deleteExamFinishAnswerById(String examCode)
    {
        return examFinishAnswerMapper.deleteExamFinishAnswerById(examCode);
    }


	@Override
	public int intoExamFinishAnswer(ExamFinishAnswer examFinishAnswer) {
        String no = UUIDGenerator.generate();
        String examCode = examFinishAnswer.getExamCode();
        Long status =0l;
        ExamTaskManager examTaskManager = examTaskManagerService.selectExamTaskManagerById(examCode);
        if (ObjectUtil.isNotNull(examTaskManager)){
            status = examTaskManager.getStatus();
        }

        if (status==0l){
            examFinishAnswerMapper.deleteExamFinishAnswer(examFinishAnswer);

        }
        // TODO Auto-generated method stub
        examFinishAnswer.setNo(no);
        int result = examFinishAnswerMapper.intoExamFinishAnswer(examFinishAnswer);

		// 维护成绩数据
    	ExamUserScore examUserScore = new ExamUserScore();
    	examUserScore.setExamCode(examCode);
    	examUserScore.setUserCode(examFinishAnswer.getUserCode());
        examUserScore.setStatus(status.intValue());
        examUserScore.setNo(no);
    	result = examUserScoreService.intoExamUserScore(examUserScore);

        if (status==0l){
            // 更新状态
            ExamTaskPerson examTaskPerson = new ExamTaskPerson();
            examTaskPerson.setExamCode(examCode);
            examTaskPerson.setuserCode(examFinishAnswer.getUserCode());
            examTaskPersonService.updateDoneStatus(examTaskPerson);
        }
        if (status==1l){

            ExamTaskAnswer examTaskAnswer = new ExamTaskAnswer();
            examTaskAnswer.setUserCode(examFinishAnswer.getUserCode());
            examTaskAnswer.setExamCode(examCode);
            examTaskAnswerService.deleteExamTaskAnswerByUser(examTaskAnswer);

        }
    	return result;		 
	}

	@Override
	public int deleteExamFinishAnswer(ExamFinishAnswer examFinishAnswer) {
		// TODO Auto-generated method stub
		return examFinishAnswerMapper.deleteExamFinishAnswer(examFinishAnswer);
	}
}
